<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('functions.php'); //Require/include the functions file
    require_once('db.php'); //Require/include the file with the database connection information
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['updatefile'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Update a file'));
    } else {
        $id = mysql_escape_string($_GET['id']);
        //Connect DB using db.php
        $db = new my_db;
        template_headtag("Update file");
        template_header();
        template_left();
        $db->query("SELECT * FROM trackedfiles WHERE id = '$id'");
         
        if ($db->nf() > 0) {
            $db->next_record();
             
            $filepath = $db->f('filepath');
            $filename = $db->f('filename');
            $latestrev = $db->f('latestrev');
             
            echo "<div class='pageheadertext'>Update $filename in</div>";
            echo "<div class='subheadertext'>$filepath</div><br/>";
             
            $status = updatefilestatus ($latestrev, "$filepath$filename");
             
            if ($_POST['submitted']) {
                $revsummary = mysql_escape_string($_POST['revsummary']);
                if ($status == 'badfile') {
                    if ($_SESSION['userlevel'] >= $userlevel['stoptrackingfile']) {
                        echo "<br/><a href='stoptrackingfile.php?id=$id'>Stop tracking file</a>";
                    }
                } elseif($status == 'nochange') {
                    errorbox("Version in DB is the latest version");
                } elseif($status == 'cannotread') {
                    errorbox("Can not read file, permission denied!");
                } elseif($status == 'isdir') {
                    errorbox("Can not be tracked, is a directory!");
                } else {
                    echo "<div align='center'>";
                    echo updatefile ($id, $status, $revsummary, $filepath, $filename);
                    echo "</div><br/>";
                     
                }
                 
            } else {
                 
                 
                echo "<div align='center'>";
                 
                if ($status == 'badfile') {
                    errorbox("File does not exist");
                    if ($_SESSION['userlevel'] >= $userlevel['stoptrackingfile']) {
                        echo "<br/><a href='stoptrackingfile.php?id=$id'>Stop tracking file</a>";
                    }
                } elseif($status == 'nochange') {
                    errorbox("Version in DB is the latest version");
                } else {
                    echo "<form method='post' action='update.php?id=$id'> ";
                    echo "Revision Summary: <input type='text' name='revsummary'/>";
                    echo "<input type='hidden' name='submitted' value='TRUE'/><br/>";
                    echo "<input type='submit' value='Update File'/>";
                    echo "</form>";
                     
                }
                 
                echo "</div><br/>";
            }
        } else {
            errorbox('Invalid file id');
            echo "<div align='center'><br/><a href='javascript:history.back(1)'>Back</a></div>";
        }
        template_footer();
         
    }
?>
